package com.atcd.springcloud.controller;


import com.atcd.springcloud.entities.Payment;
import com.atcd.springcloud.entities.ResponseResult;
import com.atcd.springcloud.lb.LoadBalance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class orderController {

    //自己写的轮训算法
    @Resource
    private LoadBalance loadBalance;
    //eureka的服务注册与发现
    @Resource
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;

//    public  static  final String PAYMENT_URL="http://localhost:8001"; 单机版

    public  static  final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE"; //微服务名称:CLOUD-PAYMENT-SERVICE为provider的两个服务

    @GetMapping("/consumer/payment/create")
    public ResponseResult<Payment> create(Payment payment){
        //发送生成者的post方法
        log.info("访问的payment:{}",payment);
       return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,ResponseResult.class);
    }


    @GetMapping("/consumer/payment/get/{id}")
    public  ResponseResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);
    }


    //写一下responseEntity    postForEntity也是一样的
    @GetMapping("/consumer/getForEtity/get/{id}")
    public  ResponseResult<Payment> getPayment1(@PathVariable("id") Long id){
        ResponseEntity<ResponseResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, ResponseResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }
        else{
             return new ResponseResult(444,"操作失败");
        }
    }

    @GetMapping("/consumer/payment/lb")
    public  String getPaymentLB(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
     if(instances==null || instances.size() <=0){
         return  null;
     }
     ServiceInstance serviceInstance =loadBalance.instance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }


    // ====================> zipkin+sleuth 测试
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }
}
